﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using System.Configuration;

public partial class Posts_Menu : BaseUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }
    }

    public override void DataBind()
    {
        if (Page is BasePostPage)
        {
            BasePostPage tmp = Page as BasePostPage;
            if (tmp.CurrentPost != null)
            {
                dlVersions.DataSource = Post.GetCurrentVersions(tmp.AcmsContext.SiteId, tmp.CurrentPost.PostGuid);
                dlVersions.DataBind();
            }

            if(tmp.CurrentPost != null)
            {
                cmsVersionControlInfoPopUp.Visible = true;
                lnkPreview.Text = string.Format("Preview '{0}'", tmp.CurrentPost.DisplayName);
                lnkPreview.NavigateUrl = string.Concat("http://", Page.AcmsContext.SiteRoot, "/", tmp.CurrentPost.Url.Substring(2));
                lnkPreview.Visible = true;
            }
            else
            {
                cmsVersionControlInfoPopUp.Visible = false;
                lnkPreview.Visible = false;
            }
        }
    }

    protected void dlVersions_ItemDataBound1(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        Post post = e.DataItem as Post;
        HyperLink link = e.Item.FindControl("lnkVersion") as HyperLink;
        if (link != null)
        {
            if (post.PublishFrom == null)
            {
                link.Text = "Pending Version";
                link.CssClass = "cmsVersionControlStandard";
            }
            else if (post.PublishFrom.Value < DateTime.Now)
            {
                link.Text = "Live Version";
                link.CssClass = "cmsVersionControlStandard";
            }
            else
            {
                link.Text = string.Format("{0} Version", post.PublishFrom.Value.ToString("dd MMM yy HH:mm"));
            }
            if (Page.Request.Url.AbsolutePath.ToLower().Contains("default.aspx"))
            {
                link.NavigateUrl = string.Format(Page.ResolveUrl("Details.aspx"));
            }
            else
            {
                link.NavigateUrl = string.Format(Page.ResolveUrl(""));
            }
            if(link.NavigateUrl.Contains("?Id="))
            {
                link.NavigateUrl = link.NavigateUrl.Substring(0, link.NavigateUrl.IndexOf("?"));
            }
            link.NavigateUrl = string.Concat(link.NavigateUrl, "?Id=", post.Id, "&SV=", post.PublishFrom == null ? "" : Server.UrlEncode(post.PublishFrom.Value.ToString("yyyy/MM/dd HH:mm")));
            if (post.Id == ((BasePostPage)Page).PostId)
            {
                link.CssClass = "cmsVersionControlStandard selected";
            }
        }
    }
}
